<?php

namespace app\admin\controller;

use app\index\model\User;
use think\Request;
use app\admin\model\Inquiry as InquiryModel;
use app\admin\model\Repertory as RepertoryModel;
use app\index\model\User as UserModel;

class Inquiry extends Base
{

    private $inquiry;
    private $repertory;
    private $user;

    function initialize()
    {
        parent::initialize();
        $this->inquiry = new InquiryModel();
        $this->repertory = new RepertoryModel();
        $this->user = new UserModel();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $inquiries = InquiryModel::all();

        // 拼接参数
        foreach ($inquiries as $k => $v) {

            $repertory = RepertoryModel::get($v['reper_id']);
            $user = UserModel::get($v['user_id']);

            $v['ask_time'] = date('Y-m-d H:i:s', $v['ask_time']);
            $v['reper_name'] = $repertory['name'];
            $v['user_name'] = $user['user_name'];

        }

        $this->assign('inquiries', $inquiries);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        // 拼接数据
        $inquriy = InquiryModel::get($id);

        $inquriy['user_name'] = UserModel::where('id', $inquriy['user_id'])->value('user_name');
        $inquriy['reper_name'] = RepertoryModel::where('id', $inquriy['reper_id'])->value('name');
        $inquriy['ask_time'] = date('Y-m-d H:i:s', $inquriy['ask_time']);
        return $inquriy;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // 询价详情
        $inquiry = InquiryModel::get($id);

        $uid = $inquiry['user_id'];
        $repertoryId = $inquiry['reper_id'];

        // 询价的用户和询价的仓名称
        $username = UserModel::where('id', $uid)->value('user_name');
        $reper_name = RepertoryModel::where('id', $repertoryId)->value('name');

        return ['username' => $username, 'reper_name' => $reper_name, 'reper_id' => $repertoryId];
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $param = input('put.');
        unset($param['_method']);

        // 修改对应仓的信息
        $reper_id = $param['reper_id'];
        unset($param['reper_id']);
        $resultReper = RepertoryModel::where('id', $reper_id)->update($param);

        // 修改询价信息
        $param['status'] = 2;
        $resultInquiry = InquiryModel::where('id', $id)->update($param);

        if ($resultReper && $resultInquiry) {
            return redirect('/inquiry');
        }
        $this->error('回执失败!');
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

}
